import { defineStore } from 'pinia'
import { ref } from 'vue'
import { useTokenStore } from './token.js'

export const useAdminStore = defineStore('admin', () => {
  // 管理员信息
  const adminInfo = ref(null)

  // 设置管理员信息
  const setAdminInfo = (info) => {
    adminInfo.value = info
  }

  // 获取管理员信息
  const getAdminInfo = () => {
    return adminInfo.value
  }

  // 清除管理员信息
  const clearAdminInfo = () => {
    adminInfo.value = null
  }

  // 检查是否已登录
  const isLoggedIn = () => {
    return adminInfo.value !== null
  }

  // 登出功能 - 清除所有相关状态
  const logout = () => {
    // 清除管理员信息
    adminInfo.value = null
    
    // 清除token
    const tokenStore = useTokenStore()
    tokenStore.removeToken()
    
    // 清除localStorage中的相关数据
    localStorage.removeItem('token')
    localStorage.removeItem('adminInfo')
  }

  return {
    adminInfo,
    setAdminInfo,
    getAdminInfo,
    clearAdminInfo,
    isLoggedIn,
    logout
  }
}, {
  // 开启持久化存储
  persist: true
})

